//
//  XHRPersonInfoVC.m
//  快医
//
//  Created by 孙博 on 16/7/6.
//
//

#import "XHRPersonInfoVC.h"

#define kWidth self.view.frame.size.width

@interface XHRPersonInfoVC ()<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>

// 图片框的背景
@property (nonatomic, weak)UIView *bgView;

// 头像的图片框
@property (nonatomic, weak)UIImageView *icon;

// table
@property (nonatomic, weak)UITableView *tv_info;

/**存储cell*/
@property (nonatomic,weak)UITableViewCell *cell;

/**文本框信息*/
@property (nonatomic,copy)NSString *alertTextFileContent;



@end

@implementation XHRPersonInfoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"个人信息";
    
    [self.tv_info registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    [self setupUI];
    [self setupFrame];
    
    //MARK: - 隐藏多余行的cell下滑线
    self.tv_info.tableFooterView = [[UIView alloc] init];
    
    self.tv_info.delegate = self;
    self.tv_info.dataSource = self;
}

- (void)setupUI{
    
    UIView *bgView = [[UIView alloc]init];
    self.bgView = bgView;
    [self.view addSubview:bgView];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self.bgView makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(0);
        make.width.mas_equalTo(kWidth);
        make.height.mas_equalTo(150);
    }];
    
    // 在bgView上加入图片框
    CGFloat x = (kWidth - 150)*0.5;
    UIImageView *icon = [[UIImageView alloc]initWithFrame:CGRectMake(x, 0, 150, 150)];
    self.icon = icon;
    icon.layer.cornerRadius = 75;
    icon.layer.masksToBounds = YES;
    // MARK:     点按换相册图片
    icon.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc ]initWithTarget:self action:@selector(tapIcon:)];
    
    [icon addGestureRecognizer:tap];
    
    icon.image = [UIImage imageNamed:@"banner"];
    
    [self.bgView addSubview:icon];
    
    // 添加table
    UITableView *table = [[UITableView alloc]init];
    self.tv_info = table;
    [self.view addSubview:table];
}

// 点按图片框
- (void)tapIcon:(UITapGestureRecognizer *)tap{
//    UIImagePickerControllerSourceTypeSavedPhotosAlbum
    UIImagePickerController *pick = [[UIImagePickerController alloc]init];
    pick.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    pick.delegate = self;
    
    [self presentViewController:pick animated:YES completion:nil];
}

#pragma
#pragma mark -PICK的代理
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{

    NSLog(@"%@",info);
    
    UIImage *img = info[@"UIImagePickerControllerOriginalImage"];
    
    
    self.icon.image = img;
    
    // 回退到app界面
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    NSLog(@"MDZZ,都进来了也不选头像");
    // 回退到app界面
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)setupFrame{
    
    [self.tv_info makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.bgView.bottom);
        make.width.mas_equalTo(kWidth);
        make.bottom.mas_equalTo(0);
    }];
}

#pragma
#pragma mark -datasource代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    
    if (indexPath.row == 0) {
        cell.textLabel.text = @"用户名:";
        cell.detailTextLabel.text = @"jack";
    }else if(indexPath.row == 1){
        cell.textLabel.text = @"注册号码:";
        cell.detailTextLabel.text = @"4462354657";
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 40;
}

#pragma mark
#pragma mark - 点击cell修改
//点击某一cell 就出现警示框
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //MARK: - 点击某一行弹框
    UIAlertController * alertVc = [UIAlertController alertControllerWithTitle:@"编辑" message:@"修改个人信息" preferredStyle:UIAlertControllerStyleAlert];
    
    //MARK: -  只有UIAlertControllerStyleAlert 这种模式可以添加文本框
    if (alertVc.preferredStyle == UIAlertControllerStyleAlert) {
        
        //添加输入框
        [alertVc addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
            
      //监听文本框的改变
        [textField addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventEditingChanged];
        }];
}
     //MARK: - 添加确定和取消
    UIAlertAction * sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.detailTextLabel.text = self.alertTextFileContent;
    }];
    UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    //MARK: - 显示警示框
    [alertVc addAction:sure];
    [alertVc addAction:cancel];
    [self presentViewController:alertVc animated:YES completion:^{
        
    }];
}

#pragma mark
#pragma mark - 文本框的改变
//这里可以通过发送object消息获取注册时指定的UITextField对象
- (void)valueChange:(UITextField *)textField
{
     self.alertTextFileContent = textField.text;
}

@end
